#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1e5 + 10;

int n, m;
int p[N], num[N];
int w[N], wp[N];

unordered_map<string, int > mp;
int idx = 1;

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

inline void solution() {
    mm(wp, 0);
    for (int i = 1; i < N; i ++ ) p[i] = i, num[i] = 1;
    cin >> n >> m;
    int tot = 0;
    for (int i = 1; i <= n; i ++  ) 
    {
        string a, b;
        int c;
        cin >> a >> b >> c;
       
        int pa, pb;
        if (mp.find(a) == mp.end()) {
            mp[a] = idx ++ ;
        }

        if (mp.find(b) == mp.end()) {
            mp[b] = idx ++ ;
        }
        int p1 = find(mp[a]), p2 = find(mp[b]);
 
        if (p1 != p2) {
            p[p2] = p1;
            num[p1] += num[p2];
            wp[p1] += wp[p2] + c;
        } else {
            wp[p1] += c;
        }
        w[mp[a]] += c;
        w[mp[b]] += c;
    }


    
    unordered_map<int, pair<string, int> > p_mp;
    for (auto & x : mp) {
        int parent = find(x.second);
        if (wp[parent] > m && num[parent] >= 3) {
            if(w[mp[p_mp[parent].first]] < w[mp[x.first]]) {
                p_mp[parent] = mk(x.first, num[parent]);
            } 
        } 
    }

    vector<pair<string,int>> v;
    if (p_mp.size()) {
        for (auto & x : p_mp) {
            auto pr = x.second;
            v.push_back({pr.first, pr.second});
        }
    }
    sort(v.begin(), v.end());
    cout << v.size() << endl;
    for (auto& x : v) {
        cout << x.first << " " << x.second << endl;
    }
    return;
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}